<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class BaseController extends Controller
{
    /**
     * @var page
     * 当前页数
     */
    public $page = '';

    /**
     * @var size
     * 每页显示的条数
     */
    public $size = '';

    /**
     * @var int
     * 从那一页开始做查询
     */
    public $from = 0;


    /**
     * @author LF
     * @abstract 判断当前的用户是否已经做了登录
     * @param Request $request
     */
    public  function isLogin(Request $request){
        $_objSession = $request->getSession();
        $_objUser = $_objSession->get($this->getParameter('user_session_scope'));
        if($_objUser && $_objUser->getId()){
            //当前是用户登录状态，不用做任何操作
        }else{
            //当前用户没有登录的情况下，跳转到的登录页面
            return $this->redirect('login');
        }
    }

    /**
     * @author lf
     * @abstract 获取当前的页和每页显示的条数
     */
    public function getPageAndSize($data){
        $this->page = !empty($data['page'])?$data['page']:1;//当前页
        $this->size = !empty($data['size'])?$data['size']:$this->getParameter('list_rows');//每页显示的条数
        $this->from = ($this->page - 1)*$this->size;//从那一页开始
    }

    /**
     * @author lf
     * @abstract 处理&转为&amp;的情况
     * @param $arrData
     */
    public function build_query($arrData){
        foreach($arrData as $key=>$value){
            if(strpos($key,'amp;') !== false){//存在
                $_strNewKey = str_replace('amp;','',$key);
                $arrData[$_strNewKey] = $value;
                unset($arrData[$key]);
            }
        }
        return $arrData;
    }
}
